package com.ex.exporttool.service.impl;

import com.ex.exporttool.entity.PcBonusCalculateTemplate;
import com.ex.exporttool.mapper.ExportMapper;
import com.ex.exporttool.service.ExportService;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import java.io.*;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/7/31
 */
@Service
public class ExportServiceImpl implements ExportService {

    @Resource
    private ExportMapper exportMapper;

    @Override
    public boolean exportBonusPackageTemplate(String month) {
        InputStream is = null;
        FileOutputStream os = null;
        try {
            File destDir = new File("D:/template/奖金包测算模板_" + month + ".xlsx");
            destDir.createNewFile();
            os = new FileOutputStream(destDir);
            is = getClass().getClassLoader().getResourceAsStream("template/bonus_package_template.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook(is);
            // 进行模板的克隆(接下来的操作都是针对克隆后的sheet)
            XSSFSheet sheet = workbook.cloneSheet(1);
            // 移除workbook中的模板sheet
            workbook.removeSheetAt(1);
            // 给sheet命名
            workbook.setSheetName(1, "计算表");
            int endNum = sheet.getPhysicalNumberOfRows();
            List<PcBonusCalculateTemplate> templateList = exportMapper.selectBonusTemplateListByMonth(month);
            // 替换
            for (int i = 1; i < endNum; i++) {
                XSSFRow row = sheet.getRow(i);
                if (row == null) {
                    continue;
                }
                XSSFCell cell = row.getCell(5);
                cell.setCellType(CellType.STRING);
                String projectName = cell.getStringCellValue();
                replaceCellValue(row.getCell(8), BigDecimal.ZERO);
                replaceCellValue(row.getCell(10), BigDecimal.ZERO);
                for (PcBonusCalculateTemplate template : templateList) {
                    String projectId = template.getProjectId();
                    BigDecimal amountTotal = template.getAmountTotal();
                    BigDecimal signAmount = template.getSignAmount();
                    if (template.getProjectName().equals(projectName)) {
                        replaceCellValue(row.getCell(4), projectId);
                        replaceCellValue(row.getCell(8), amountTotal);
                        replaceCellValue(row.getCell(10), signAmount);
                    }
                }


            }
            workbook.write(os);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 替换内容
     *
     * @param cell  单元格
     * @param value 需要替换的值
     */
    private void replaceCellValue(XSSFCell cell, Object value) {
        if (value instanceof String) {
            cell.setCellValue((String) value);
        } else if (value instanceof BigDecimal) {
            double val = ((BigDecimal) value).doubleValue();
            cell.setCellValue(val);
        } else {
            cell.setCellValue("");
        }
    }
}
